package com.mhc.haval.service.impl;

import com.mhc.haval.dal.model.WorkflowDO;
import com.mhc.haval.dal.query.WorkflowQuery;
import com.mhc.haval.manager.WorkflowManager;
import com.mhc.haval.service.WorkFlowService;
import com.mhc.suzuki.constant.IsDeletedEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by kanon on 2017/8/24.
 */
@Slf4j
@Service
@Transactional(rollbackFor = Exception.class)
public class WorkFlowServiceImpl implements WorkFlowService {

    @Autowired
    private WorkflowManager workflowManager;


    @Override
    public Boolean addWorkflow(WorkflowDO workflowDO) {
        Boolean result;
        workflowDO.setIsDeleted(IsDeletedEnum.NORMAL.getCode());
        long workflowId = workflowManager.insertSelective(workflowDO);
        if (workflowId > 0) {
            result = Boolean.TRUE;
        } else {
            result = Boolean.FALSE;
        }
        return result;
    }

    @Override
    public Boolean deleteWorkflow(Long workflowId) {
        Boolean result;
        WorkflowDO workflowDO = new WorkflowDO();
        workflowDO.setWorkflowId(workflowId);
        workflowDO.setIsDeleted(IsDeletedEnum.DELETED.getCode());
        int updateCount = workflowManager.updateByPrimaryKeySelective(workflowDO);
        if (updateCount > 0) {
            result = Boolean.TRUE;
        } else {
            result = Boolean.FALSE;
        }
        return result;
    }


    @Override
    public Boolean modifyWorkflow(WorkflowDO workflowDO) {
        Boolean result;
        int updateCount = workflowManager.updateByPrimaryKeySelective(workflowDO);
        if (updateCount > 0) {
            result = Boolean.TRUE;
        } else {
            result = Boolean.FALSE;
        }
        return result;
    }

    @Override
    public List<WorkflowDO> getAllWorkflow(String companyNo) {
        WorkflowQuery workflowQuery = new WorkflowQuery();
        workflowQuery.createCriteria().andIsDeletedEqualTo(IsDeletedEnum.NORMAL.getCode())
                .andCompanyNoEqualTo(companyNo);
        return workflowManager.selectByQuery(workflowQuery);
    }

    @Override
    public WorkflowDO getWorkflow(Long workflowId) {
        return workflowManager.selectByPrimaryKey(workflowId);
    }

}
